/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package datamodel;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

/**
 *
 * @author Kvest
 */
public class District 
{
    private int id;
    private String name;
    private int driversCount;
    private int ordersCount;
    private int number; 
    
    public District(int id, String name)
    {
        this.id = id;
        this.name = name;
        driversCount = 0;
        ordersCount = 0;
        number = 0;
    }
    
    public District(byte[] data) throws IOException
    {
        driversCount = 0;
        ordersCount = 0;
        
        ByteArrayInputStream bais = new ByteArrayInputStream(data);
        DataInputStream dis = new DataInputStream(bais);

        try
        {
            id = dis.readInt();
            name = dis.readUTF();
            
            number = dis.readInt();
        }
        finally
        {
            try
            {
                dis.close();
                bais.close();
            }catch(Exception ex){}
        }    
    }
    
    public byte[] toBytes()
    {
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(result);
        
        try
        {
            dos.writeInt(id);
            dos.writeUTF(name);
            dos.writeInt(number);
                    
            return result.toByteArray();
        }
        catch(IOException ioex)
        {
            return (new byte[]{});
        }
        finally
        {
            try
            {
                dos.close();
                result.close();
            }catch(Exception ex){}
        }
        
    }

    public int getId() 
    {
        return id;
    }

    public void setId(int id) 
    {
        this.id = id;
    }

    public String getName() 
    {
        return name;
    }

    public void setName(String name) 
    {
        this.name = name;
    }

    public int getDriversCount() 
    {
        return driversCount;
    }

    public void setDriversCount(int driversCount) 
    {
        this.driversCount = driversCount;
    }

    public int getOrdersCount() 
    {
        return ordersCount;
    }

    public void setOrdersCount(int ordersCount) 
    {
        this.ordersCount = ordersCount;
    }
    
    public String getFullDescription()
    {
        return ((driversCount < 10 ? "0" : "") + driversCount + "-" + (ordersCount < 10 ? "0" : "") + ordersCount + " " + name);
    }

    public int getNumber() 
    {
        return number;
    }

    public void setNumber(int number) 
    {
        this.number = number;
    } 
}
